/*
 * MemorySequence.java
 *
 * Created on February 26, 2007, 2:02 PM
 *
 * (C) R. Alexander Milowski alex@milowski.com
 */

package org.infoset.xml.memory;

import org.infoset.xml.Infoset;
import org.infoset.xml.Item;
import org.infoset.xml.Name;
import org.infoset.xml.TypeDefinition;
import org.infoset.xml.Value;

/**
 *
 * @author R. Alexander Milowski
 */
public class MemoryValue extends MemoryItem implements Value {

   protected String lexicalValue;
   protected Object value;
   protected TypeDefinition typeDefinition;
   protected Name typeName;
   
   /** Creates a new instance of MemorySequence */
   public MemoryValue(Infoset infoset,Object value) {
      super(infoset,0,null);
      this.value = value;
      this.lexicalValue = null;
   }
   
   public Item.ItemType getType() {
      return Item.ItemType.SequenceItem;
   }
   
   protected void setIndex(int i) {}
   
   public Item copyOfItem(boolean deep) {
      MemoryValue newValue = new MemoryValue(infoset,value);
      newValue.lexicalValue = lexicalValue;
      newValue.typeDefinition = typeDefinition;
      newValue.typeName = typeName;
      return newValue;
   }
   
   public Object getTypedValue()
   {
      return value;
   }

   public void setTypedValue(Object value) {
      this.value = value;
      this.lexicalValue = null;
   }
   
   public String getNormalizedText()
   {
      return getText();
   }
   
   public String getText() {
      return lexicalValue==null ? value.toString() : lexicalValue;
   }

   public void setText(String value) {
      throw new RuntimeException("Values are immutable via the textual value.");
   }
   
   public Name getTypeName() {
      return typeName;
   }
   
   public void setTypeName(Name typeName) {
      this.typeDefinition = null;
      this.typeName = typeName;
   }
    
   public TypeDefinition getTypeDefinition() {
      return typeDefinition;
   }
   
   public void setTypeDefinition(TypeDefinition typeDef) {
      typeDefinition = typeDef;
      typeName = typeDef==null ? typeDef.getName() : null;
   }
}
